<template>
  <div class="app-container">
    <el-card class="mb20">
      <el-form :model="listQ" inline>
        <el-form-item label="流水号">
          <el-input v-model="listQ.serialNo" placeholder="流水号"></el-input>
        </el-form-item>
        <el-form-item label="计划编号">
          <el-input v-model="listQ.planNo" placeholder="计划编号"></el-input>
        </el-form-item>
        <el-form-item label="作业名称">
          <el-input v-model="listQ.jobName" placeholder="作业名称"></el-input>
        </el-form-item>
        <el-form-item label="物料名称">
          <el-input
            v-model="listQ.materialName"
            placeholder="物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="检验项">
          <el-input
            v-model="listQ.inspectionItem"
            placeholder="检验项"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getList">搜索</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card>
      <el-button type="success" @click="handleAdd">新增</el-button>
      <el-table :data="list">
        <el-table-column
          v-for="item in listC"
          :key="item.prop"
          :label="item.label"
          :prop="item.prop"
        ></el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          v-slot="{ row }"
          width="200px"
        >
          <el-button type="primary" @click="handleEdit(row)" size="mini"
            >修改</el-button
          >
          <el-button type="danger" @click="handleDel(row.id)" size="mini"
            >删除</el-button
          >
        </el-table-column>
      </el-table>
      <el-pagination
        style="text-align: right; margin-top: 20px"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pager.page"
        :page-sizes="[2, 10, 20, 30]"
        :page-size="pager.limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pager.total"
      >
      </el-pagination>
    </el-card>
    <el-dialog :title="title" center :visible.sync="visible" @close="close">
      <el-form inline ref="listERef" :model="listE">
        <el-form-item label="作业名称" prop="jobName" required>
          <el-input
            disabled
            v-model="listE.jobName"
            placeholder="作业名称"
          ></el-input>
        </el-form-item>
        <el-form-item
          ><el-button @click="openIssueDialog" type="primary"
            >选择</el-button
          ></el-form-item
        >
        <el-form-item label="物料名称" prop="materialName" required>
          <el-input
            v-model="listE.materialName"
            placeholder="物料名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="采样数量" prop="quantity" required>
          <el-input v-model="listE.quantity" placeholder="采样数量"></el-input>
        </el-form-item>
        <el-form-item label="检验标准" prop="standardName" required>
          <el-input
            disabled
            v-model="listE.standardName"
            placeholder="检验标准"
          ></el-input>
        </el-form-item>
        <el-form-item
          ><el-button @click="openIssueDialog2" type="primary"
            >选择</el-button
          ></el-form-item
        >

        <el-form-item label="检验值" prop="inspectionValue" required>
          <el-input
            v-model="listE.inspectionValue"
            placeholder="检验值"
          ></el-input>
        </el-form-item>
        <el-form-item label="检验结果" prop="inspectionResult" required>
          <el-input
            v-model="listE.inspectionResult"
            placeholder="检验结果"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            type="textarea"
            :rows="3"
            resize="none"
            v-model="listE.remark"
            placeholder="备注"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <div>
          <el-button @click="close">取消</el-button>
          <el-button type="primary" @click="submit">确定</el-button>
        </div>
      </template>
    </el-dialog>
    <el-dialog
      title="选择生产作业"
      center
      :visible.sync="visibleIssue"
      @close="visibleIssue = false"
    >
      <el-table :data="issueList">
        <el-table-column label="作业名称" prop="jobName"></el-table-column>
        <el-table-column label="型号" prop="productModel"></el-table-column>
        <el-table-column label="计划号" prop="planNo"></el-table-column>
        <el-table-column label="产品名称" prop="productName"></el-table-column>
        <el-table-column
          label="产品规格"
          prop="productSpecification"
        ></el-table-column>
        <el-table-column label="产品单位" prop="productUnit"></el-table-column>
        <el-table-column v-slot="{ row }">
          <el-button type="primary" @click="selectIssue(row)">选择</el-button>
        </el-table-column>
      </el-table>
    </el-dialog>
    <el-dialog
      title="选择检验标准"
      center
      :visible.sync="visible2"
      @close="visible2 = false"
    >
      <el-table :data="tandardList">
        <el-table-column label="描述" prop="description"></el-table-column>
        <el-table-column label="阈值下限" prop="lowerLimit"></el-table-column>
        <el-table-column label="检验标准" prop="name"></el-table-column>
        <el-table-column label="阈值上限" prop="upperLimit"></el-table-column>
        <el-table-column label="标准值" prop="value"></el-table-column>
        <el-table-column v-slot="{ row }">
          <el-button type="primary" @click="selectIssue2(row)">选择</el-button>
        </el-table-column>
      </el-table>
    </el-dialog>
  </div>
</template>

<script>
import { getStandardList } from '@/api_zc/mes/quality-management/product-inspection'
import { getProductSamplingList, getJobList, editProductSampling, addProductSampling, delProductSampling } from '@/api_zc/mes/quality-management/production-sampling'
export default {
  mounted () {
    this.getList()
    this.getUnit()
  },
  data () {
    return {
      list: [],
      keyword: '',
      listQ: {
        planNo: '',
        materialName: "",
        jobName: "",
        inspectionBatch: "",
        inspectionItem: "",
        material: "",
        serialNo: "",
        status: "",
      },
      listC: [
        {
          label: '编号',
          prop: 'id'
        },
        {
          label: '流水号',
          prop: 'serialNo'
        },
        {
          label: '计划编号',
          prop: 'planNo'
        },
        {
          label: '作业名称',
          prop: 'job.jobName'
        },
        {
          label: '物料名称',
          prop: 'materialName'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '检验项',
          prop: 'inspectionItem'
        },
        {
          label: '生产数量',
          prop: 'productionQuantity'
        },
        {
          label: '采样数量',
          prop: 'quantity'
        },
        {
          label: '检验值',
          prop: 'inspectionValue'
        },
        {
          label: '阈值上限',
          prop: 'standard.upperLimit'
        },
        {
          label: '阈值下限',
          prop: 'standard.lowerLimit'
        },
        {
          label: '检验结果',
          prop: 'inspectionResult'
        },
        {
          label: '检验人',
          prop: 'inspector'
        },
        {
          label: '检验时间',
          prop: 'inspectionDate'
        },
      ],
      listE: {
        id: null,
        jobId: null,
        jobName: null,
        materialName: null,
        quantity: null,
        inspectionValue: null,
        inspectionResult: null,
        remark: null,
        standardName: null,
        standardId: null
      },
      detailListC: [
        {
          label: '物料',
          prop: 'material'
        },
        {
          label: '型号',
          prop: 'model'
        },
        {
          label: '规格',
          prop: 'specification'
        },
        {
          label: '单位',
          prop: 'unit'
        },
        {
          label: '需求数量',
          prop: 'demandAmount'
        },
        {
          label: '已用数量',
          prop: 'usedAmount'
        },
        {
          label: '剩余数',
          prop: 'remainAmount'
        },
        {
          label: '备注',
          prop: 'remark'
        },
      ],
      pager: {
        limit: 10,
        page: 1,
        total: 0
      },
      visible: false,
      title: '',
      isAdd: false,
      issueList: [],
      visibleIssue: false,
      detailList2: [],
      flag: false,
      tandardList: [],
      visible2: false
    }
  },
  methods: {
    async getUnit () {
      const { data: { content } } = await getJobList()
      this.issueList = content
      const data1 = await getStandardList()
      this.tandardList = data1.data.content
    },
    async getList () {
      const { data: { content, total } } = await getProductSamplingList({ ...this.listQ, keyword: this.keyword, page: this.pager.page, limit: this.pager.limit })
      this.list = content
      this.pager.total = total
    },
    handleDisabled (flag) {
      if (this.isAdd) {
        return false
      }
      if (this.listE.publishStatus == '已发布' || (this.listE.publishStatus == '未发布' && flag)) {
        return true
      }
      return false
    },
    handleReset () {
      Object.assign(this.listQ, this.$options.data().listQ)
      this.getList()
    },
    handleSizeChange (val) {
      this.pager.limit = val
      this.getList()
    },
    handleCurrentChange (val) {
      this.pager.page = val
      this.getList()
    },
    close () {
      this.visible = false
    },
    handleEdit (row) {
      this.visible = true
      this.title = "修改产品检验单"
      this.isAdd = false
      this.$nextTick(() => {
        Object.assign(this.listE, {
          id: row.id,
          jobId: row.job.id,
          jobName: row.job.jobName,
          materialName: row.materialName,
          quantity: row.quantity,
          inspectionValue: row.inspectionValue,
          inspectionResult: row.inspectionResult,
          remark: row.remark,
          standardName: row.standard.name,
          standardId: row.standard.id
        })
      })
    },
    handleAdd () {
      this.visible = true
      this.title = "修改来料检验"
      this.isAdd = true
      this.$nextTick(() => {
        this.$refs.listERef.resetFields()
      })
    },
    async handleDel (id) {
      await delProductSampling({ ids: [id] })
      this.$message.success('删除成功')
      this.getList()
    },
    submit () {
      this.$refs.listERef.validate(async (callback) => {
        if (!callback) {
          return this.$message.error('请填写必要字段')
        }
        if (this.isAdd) {
          await addProductSampling(this.listE)
          this.$message.success('添加成功')
        } else {
          await editProductSampling(this.listE)
          this.$message.success('修改成功')
        }
        this.visible = false
        this.getList()
      })
    },
    openIssueDialog () {
      this.visibleIssue = true
    },
    openIssueDialog2 () {
      this.visible2 = true
    },
    selectIssue (row) {
      this.listE.jobName = row.jobName
      this.listE.jobId = row.id
      this.visibleIssue = false
    },
    selectIssue2 (row) {
      this.listE.standardName = row.name
      this.listE.standardId = row.id
      this.visible2 = false
    },
    selectMaterial (row) {
      this.listE.issueDeetailId = row.id
      this.visibleIssue = false
    },
  }
}
</script>

<style lang="scss">
</style>